#!/usr/bin/Rscript --default-packages=RMySQL,timeSeries
args <- commandArgs(TRUE)
if (length(args)==0 | length(args)>3)
  stop ("usage: echoQuote.R stocksymbol (YYYYMMDD (YYYYMMDD))")

code = args[1]
sql <- sprintf ("SELECT date,open,high,low,close,vol,oi FROM stockname LEFT JOIN stockprice ON (id=stock_id) WHERE code='%s'",code)
if (length(args)>1) {
	datefrom = args[2]
	sql <- paste (sql, sprintf ("AND date>='%s'", datefrom), SEP="")
}
if (length(args)>2) {
	dateto = args[3]
	sql <- paste (sql, sprintf ("AND date<='%s'", dateto), SEP="")
}

# exclude holidays (day without trades)
sql <- paste (sql,"HAVING !(open=high && high=low && low=close && vol=0)", SEP="")
sql <- paste (sql,"ORDER BY `date`", SEP="")
sql

con <- dbConnect(MySQL(), user="va", password="dummy", dbname="va_stratlab", host="localhost")

dset <- dbGetQuery(con, sql)

# convert dataframe dset into a timeseries object
dsetts=timeSeries(dset[2:5],as.Date(dset$date, "%Y%m%d"))
series(dsetts)

q()
